﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Cellula.Model;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Edwon.Web.Support;
using Cn.Edwon.Web;
using Cn.Cellula.Model.System;
using Cn.Cellula.Client;

namespace Cn.Cellula.Website.Admin.Content.Code {
    public partial class TypeEdit : PageSupport {
        ICodeBiz biz = BizFactory.Get<ICodeBiz>();
        String TypeID = null;
        protected void Page_Load(object sender, EventArgs e) {
            TypeID = Request.QueryString["id"];
            if (!Page.IsPostBack) {
                if (!String.IsNullOrEmpty(TypeID)) {
                    FillForm(TypeID);
                }
            }
        }

        /// <summary>
        /// 表单初始化数据
        /// </summary>
        /// <param name="typeKey"></param>
        private void FillForm(String typeKey) {
            TSysCodeTypeEO eo = biz.Load<TSysCodeTypeEO>(typeKey);
            this.txt_TypeName.Text = eo.TypeName;
            this.txt_TypeKey.Text = eo.TypeKey;
            this.hid_OldTypeName.Value = eo.TypeName;
            this.cbx_Lock.Checked = (eo.IsLocked==1);
            if (eo.IsLocked == 1) {
                this.txt_TypeKey.Enabled = false;
                this.txt_TypeName.Enabled = false;
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_save_Click(object sender, EventArgs e) {
            //---- 增加新项 ---------------------------------
            if (String.IsNullOrEmpty(TypeID)) {
                TSysCodeTypeEO eo = new TSysCodeTypeEO();
                eo.TypeName = this.txt_TypeName.Text;
                eo.TypeKey = this.txt_TypeKey.Text;
                if (biz.IfTypeNameExist(eo.TypeName)) {//检查名字是否重复
                    SiteInfo.Send(Page, TxtDef.ERROR, "名称已存在，请重新输入");
                    return;
                }
                try {
                    TSysCodeTypeEO _tmpEO = biz.Load<TSysCodeTypeEO>(eo.TypeKey);
                    if (_tmpEO != null) {
                        SiteInfo.Send(Page, TxtDef.ERROR, "值已存在，请重新输入");
                        return;
                    }
                } catch { }
                biz.Save<TSysCodeTypeEO>(eo);
                BizFactory.Get<ILogBiz>().LogInsert("代码类型", eo.TypeName, this.Actor, Request);
                SiteInfo.SendSucceed(Page, "TypeList.aspx?t=" + eo.TypeKey);
            } else {//更新----------------------------------
                TSysCodeTypeEO eo = biz.Load<TSysCodeTypeEO>(TypeID);
                if (eo.IsLocked == 1) {
                    eo.IsLocked = (cbx_Lock.Checked) ? 1 : 0;
                    biz.Update<TSysCodeTypeEO>(eo);
                } else {
                    eo.IsLocked = (cbx_Lock.Checked) ? 1 : 0;
                    eo.TypeKey = this.txt_TypeKey.Text;
                    eo.TypeName = this.txt_TypeName.Text;
                    if (this.hid_OldTypeName.Value != eo.TypeName && biz.IfTypeNameExist(eo.TypeName)) {//检查名字是否重复
                        SiteInfo.Send(Page, TxtDef.ERROR, "名称已存在，请重新输入");
                        return;
                    }
                    if (TypeID != eo.TypeKey) {//Key值有变化
                        try {
                            TSysCodeTypeEO _tmpEO = biz.Load<TSysCodeTypeEO>(eo.TypeKey);
                            if (_tmpEO != null) {
                                SiteInfo.Send(Page, TxtDef.ERROR, "值已存在，请重新输入");
                                return;
                            }
                        } catch { }
                    }
                    biz.UpdateCodeType(eo, TypeID);
                    BizFactory.Get<ILogBiz>().LogUpdate("代码类型", eo.TypeName, this.Actor, Request);
                }
                Response.Redirect("TypeList.aspx?t=" + eo.TypeKey);
                //SiteInfo.SendSucceed(Page, "TypeList.aspx?t=" + eo.TypeKey);
            }
        }
    }
}
